//
//  LoginViewController.m
//  ZhuKuang
//
//  Created by jayce on 17/7/11.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginApIModel.h"
#import "SettingsViewController.h"


@interface LoginViewController ()
/** 记住密码按钮*/
@property (weak, nonatomic) IBOutlet UIButton *remeberPwdBtn;
/** 自动登录按钮*/
@property (weak, nonatomic) IBOutlet UIButton *autoLoginBtn;
/** 用户名*/
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
/** 密码*/
@property (weak, nonatomic) IBOutlet UITextField *passworkTF;
/** 记住密码事件*/
- (IBAction)p_rememberPasswordAction:(UIButton *)sender;
/** 自动登录事件*/
- (IBAction)p_autoLoginAction:(UIButton *)sender;
/** 登录事件*/
- (IBAction)p_loginAction:(id)sender;
/** 设置事件*/
- (IBAction)p_settingsAction:(id)sender;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //设置用户名
    if ([[CommClass sharedCommon] objectForKey:LOGIN_USERNAME]) {
        self.accountTF.text=[[CommClass sharedCommon] objectForKey:LOGIN_USERNAME];
    }
    //设置记住密码按钮初始值
    if ([[CommClass sharedCommon] objectForKey:ISREMEBERPWD] == nil) {
        [[CommClass sharedCommon] setObject:@(YES) forKey:ISREMEBERPWD];
    }
    //设置记住密码按钮默认值
    if ([[[CommClass sharedCommon] objectForKey:ISREMEBERPWD] boolValue]) {
        self.passworkTF.text = [[CommClass sharedCommon] objectForKey:LOGIN_PASSWORD];
        self.remeberPwdBtn.selected = YES;
    }else{
      self.remeberPwdBtn.selected = NO;
    }
    //设置自动登录默认值
    if ([[[CommClass sharedCommon] objectForKey:ISAUTOLOGIN] boolValue]) {
        self.autoLoginBtn.selected = YES;
    }else{
        self.autoLoginBtn.selected = NO;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)p_rememberPasswordAction:(UIButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
        [[CommClass sharedCommon] setObject:@(NO) forKey:ISREMEBERPWD];
    }else{
        sender.selected = YES;
        [[CommClass sharedCommon] setObject:@(YES) forKey:ISREMEBERPWD];
    }
}

- (IBAction)p_autoLoginAction:(UIButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
        [[CommClass sharedCommon] removeObjectForKey:ISAUTOLOGIN];
    }else{
        sender.selected = YES;
        [[CommClass sharedCommon] setObject:@(YES) forKey:ISAUTOLOGIN];
    }
}

- (IBAction)p_loginAction:(id)sender {
    
    //判断用户名密码是否为空
    if (self.accountTF.text.length==0){
        [MBProgressHUD postMessageWithText:@"用户名不能为空!" View:kWindow];
        return;
    }
    if (self.passworkTF.text.length==0) {
        [MBProgressHUD postMessageWithText:@"密码不能为空!" View:kWindow];
        return;
    }
    [self.view endEditing:YES];
    //请求
    NSDictionary *dict=@{
                         
                         @"passWord":self.passworkTF.text,
                         @"loginName":self.accountTF.text,
                         @"RegistrationId":[JPUSHService registrationID]? [JPUSHService registrationID] :@""
                         };
    
    [LoginApIModel login:dict
                 success:^(NSDictionary *response) {
                     [MBProgressHUD postMessageWithText:@"登录成功!" View:kWindow];
                     [UserLoginModel setLoginWithUserName:self.accountTF.text passWord:self.passworkTF.text userInfo:response];
                     [UIApplication sharedApplication].keyWindow.rootViewController = [RootViewController createHomeViewController];                     
                 }];
}

- (IBAction)p_settingsAction:(id)sender {
    
    SettingsViewController *settings = [[SettingsViewController alloc]initWithNibName:@"SettingsViewController" bundle:nil];
    [self presentViewController:settings animated:YES completion:nil];
}

//添加监听
- (void)addKeyboardObserver{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillAppear:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillDisappear:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)keyboardWillAppear:(NSNotification *)notification {
    
    CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
    CGRect currentFrame = self.view.frame;
    //让键盘的顶部与登录按钮的底部对齐 (高度差)
    CGFloat margin = kScreenHeight - CGRectGetMaxY(self.passworkTF.frame);
    currentFrame.origin.y = 0 - change + margin;
    
    NSNumber* duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    
    [UIView animateWithDuration:[duration doubleValue]
                     animations:^{
                         
                         self.view.frame = currentFrame;
                     }
                     completion:^(BOOL finished) {
                         
                     }];
    
}

- (void)keyboardWillDisappear:(NSNotification *)notification {
    
    CGRect currentFrame = self.view.frame;
    currentFrame.origin.y = 0;
    
    NSNumber* duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    
    [UIView animateWithDuration:[duration doubleValue]
                     animations:^{
                         
                         self.view.frame = currentFrame;
                     }
                     completion:^(BOOL finished){
                         
                     }];
}

// 计算键盘的高度
- (CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo {
    
    CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    
    CGRect keyboardEndingFrame = [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil];
    
    return keyboardEndingFrame.size.height;
}

#pragma mark - textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}
@end
